﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace cURL4Net
{
    public class LibraryWrapper
    {
        [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(String path);

        [DllImport("kernel32.dll")]
        private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);

        [DllImport("kernel32.dll")]
        private extern static bool FreeLibrary(IntPtr lib);
        private IntPtr Library;

        public LibraryWrapper(String lib)
        {
            Library = LoadLibrary(lib);
        }

        ~LibraryWrapper()
        {
            FreeLibrary(Library);          
        }


        public TDelegate Invoke<TDelegate>(String method)
        {
            IntPtr api = GetProcAddress(Library, method);
            dynamic func = Marshal.GetDelegateForFunctionPointer(api, typeof(TDelegate));
            return (TDelegate)func;
        }
    }
}
